package com.sinosoft.ptas.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinosoft.framework.core.api.domain.OrganizationEntity;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.service.dao.OrganizationMapper;
import com.sinosoft.ptas.dao.NoticeMapper;
import com.sinosoft.ptas.entity.NoticeEntity;
import com.sinosoft.ptas.entity.OrganizationNoticeRefEntity;
import com.sinosoft.ptas.service.NoticeService;
import com.sinosoft.ptas.system.commom.ExceptionConstant;

/**
 * 公告接口实现
 * @author zhangxiaolong
 *
 */

public class NoticeServiceImpl implements NoticeService {

	NoticeMapper noticeMapper;
	
	@Autowired
	OrganizationMapper organizationMapper;
	
	public void setNoticeMapper(NoticeMapper noticeMapper) {
		this.noticeMapper = noticeMapper;
	}

	/**
	 * 添加公告
	 * @param noticeEntity
	 * @return id
	 */
	public String createNotice(NoticeEntity noticeEntity) {
		noticeEntity.setClickNum("0");
		noticeEntity.setDownNum("0");
		/*获取公告对象的字符串数字  转化为字符串*/
		String[] noticeObject=noticeEntity.getNoticeObject();
		String noticeObjects="";
		if(noticeObject.length!=0){
			for(int i=0;i<noticeObject.length;i++){
				noticeObjects+=noticeObject[i]+",";
			}
			String noticeObjectss=noticeObjects.substring(0,noticeObjects.length()-1);//去除最后一个逗号
			noticeEntity.setNoticeObjects(noticeObjectss);
		}else{
			noticeEntity.setNoticeObjects("未分配对象");
		}
		noticeMapper.createNotice(noticeEntity);
		return noticeEntity.getId();
	}

	/**
	 * 根据公告编号删除公告
	 * 先删除公告和司局的对应关系，再删除公告
	 * @param id
	 */
	public void deleteNoticeByid(String id) throws BusinessException{
		if(StringUtil.isBlank(id)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		noticeMapper.deleteOrgNoticeRefByNId(id);
		noticeMapper.deleteNoticeByid(id);
	}

	/**
	 * 根据公告编号更新公告
	 * @param noticeEntity
	 */
	public void updateNotice(NoticeEntity noticeEntity) {
		noticeMapper.updateNotice(noticeEntity);
	}

	/**
	 * 根据公告编号查询公告
	 * @param id
	 * @return 公告实体
	 */
	public NoticeEntity retieveNoticeByid(String id) {
		return noticeMapper.retieveNoticeByid(id);
	}

	/**
	 * 查询全部公告并降序排序
	 * @return 公告集合
	 */
	public List<NoticeEntity> retiveNotices(NoticeEntity noticeEntity) {
		
		
		
		return noticeMapper.retiveNotices(noticeEntity);
	}

	/**
	 * 批量创建公告与司局的对应关系
	 * 一个公告对应多个司局
	 * @param noticeId
	 * @param orgIds
	 * @param organizationNoticeRefEntity接收时间切面
	 */
	public void createOrgNoticeRef(OrganizationNoticeRefEntity organizationNoticeRefEntity,String noticeId,String[] orgIds) throws BusinessException{
		if(StringUtil.isBlank(noticeId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		if(orgIds.length==0){
			throw new BusinessException("您的公告没有给司局推送");
		}
		List<OrganizationNoticeRefEntity> organizationNoticeRefEntitys=new ArrayList<OrganizationNoticeRefEntity>();
		for(int i=0;i<orgIds.length;i++){
			OrganizationNoticeRefEntity onre=new OrganizationNoticeRefEntity();
			onre.setId(UUID.randomUUID().toString());
			onre.setNoticeId(noticeId);
			onre.setOrgId(orgIds[i]);
			onre.setState("未读");
			organizationNoticeRefEntitys.add(onre);
			onre.setCreater(organizationNoticeRefEntity.getCreater());
			onre.setCreateTime(organizationNoticeRefEntity.getCreateTime());
		}
		noticeMapper.createOrgNoticeRef(organizationNoticeRefEntitys);
	}
	
	/**
	 * 检索全部公告和司局的对应关系
	 * @return 公告与司局的对应关系实体集合
	 */
	public List<OrganizationNoticeRefEntity> retrieveOrgNoticeRefs(){
		return noticeMapper.retrieveOrgNoticeRefs();
	}
	
	/**
	 * 更新公告点击量
	 * 一个司局点击一次   计算为一个
	 * @param noticeId
	 * @throws BusinessException
	 */
	public void updateNoticeClickNumAndState(String noticeId,String code) throws BusinessException{
		if(StringUtil.isBlank(noticeId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		
		//根据区域编码查询司局只有一条
		OrganizationEntity organizationEntity=organizationMapper.retrieveOrganizationsByNameAndCode(null, code).get(0);
		OrganizationNoticeRefEntity organizationNoticeRefEntity=noticeMapper.retrieveOrgNoticeRefByNIdAndOId(organizationEntity.getId(),noticeId);
		
		NoticeEntity noticeEntity=noticeMapper.retieveNoticeByid(noticeId);
		//判断此省是否读过这条公告
		if(organizationNoticeRefEntity.getState().equals("未读")){
			int newClickNum=Integer.parseInt(noticeEntity.getClickNum())+1;
			noticeEntity.setClickNum(newClickNum+"");
			organizationNoticeRefEntity.setState("已读");
		}
		//更新公告状态
		noticeMapper.updateOrgNoticeRefByNIdAndOId(organizationNoticeRefEntity);
		//更新公告点击量
		noticeMapper.updateNotice(noticeEntity);
	}
	
	/**
	 * 根据公告编号删除公告与司局的对应关系
	 * @param noticeId
	 */
	public void deleteOrgNoticeRefByNId(String noticeId) throws BusinessException{
		if(StringUtil.isBlank(noticeId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		noticeMapper.deleteNoticeByid(noticeId);
	}

	/**
	 * 省局查看公告 
	 * @param orgId
	 * @return 公告实体集合
	 */
	public List<NoticeEntity> retiveNoticesByOrgId(String orgId){
		if(StringUtil.isBlank(orgId)){
			throw new BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		return noticeMapper.retiveNoticesByOrgId(orgId);
	}

	/**
	 * 查看未读公告
	 * @param orgId
	 * @return 公告条数
	 */
	public int retrieveNoReadNoticeByOrgId(String orgId) {
		return noticeMapper.retrieveNoReadNoticeByOrgId(orgId);
	}
}
